package cn.lliiooll.mirai

import cn.lliiooll.jiexi.VideoJieXi
import com.google.common.base.Strings
import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.message.GroupMessageEvent
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.MessageChainBuilder
import net.mamoe.mirai.message.data.quote

object JieXi : PluginBase() {

    override fun onEnable() {
        super.onEnable()
        VideoJieXi.log = logger
        val start = System.currentTimeMillis()// 用于记录启动时间

        var last = 0L
        subscribeAlways<GroupMessageEvent> { e ->
            if ((System.currentTimeMillis() - last) >= 2000) {
                // 避免刷屏
                last = System.currentTimeMillis();
                var msg = e.message.contentToString()
                    .replace(" ", "")
                    .replace("\n", "")
                    .replace("\t", "")
                if (msg.startsWith("#解析")) {
                    if (!Strings.isNullOrEmpty(msg.replace("#解析", ""))) {
                        // 链接不为空
                        var url = msg.replace("#解析", "")
                            .replace("https://", "")
                            .replace("http://", "")
                        if (url.startsWith("v.douyin.com")) {
                            logger.info("是抖音")
                            val dy = VideoJieXi.douyin(msg.replace("#解析", ""))
                            logger.info("解析完毕")
                            if (dy.ok) {
                                logger.info("成功")
                                var videoUrl = ""
                                var musicUrl = ""
                                var coverUrl = ""
                                var dcoverUrl = ""
                                for (element in dy.videos) {
                                    for (q in element.video.playAddr.urlList) {
                                        videoUrl = videoUrl + q + "\n";
                                    }
                                    for (m in element.music.playAddr.urlList) {
                                        musicUrl = musicUrl + m + "\n";
                                    }
                                    for (c in element.video.cover.urlList) {
                                        coverUrl = coverUrl + c + "\n";
                                    }
                                    for (dc in element.video.dynamicCover.urlList) {
                                        dcoverUrl = dcoverUrl + dc + "\n";
                                    }
                                }
                                e.group.sendMessage(
                                    e.message.quote()
                                        .plus(At(e.sender))
                                        .plus("\n=====无水印视频链接(手机打开)=====\n")
                                        .plus(videoUrl.replace("playwm", "play"))
                                        .plus("=====有水印视频链接(手机打开)=====\n")
                                        .plus(videoUrl)
                                        .plus("======背景音乐链接======\n")
                                        .plus(musicUrl)
                                        .plus("======视频封面连接======\n")
                                        .plus(coverUrl)
                                        .plus("======视频动态封面连接======\n")
                                        .plus(dcoverUrl)
                                )
                            } else {
                                e.group.sendMessage("解析失败，请检查URL是否正确")
                            }
                        } else if (url.startsWith("share.ippzone.com/pp/post")) {
                            val pp = VideoJieXi.ippzone(msg.replace("#解析", ""));
                            var q = "上传者ip: " + pp.ip + "\n";
                            var i = 0;
                            for (video in pp.video) {
                                q = q + "======视频" + i + "======\n" +
                                        "无水印:" + video.url + " \n" +
                                        "有水印:" + video.urlWm + " \n"
                            }
                            e.group.sendMessage(
                                e.message.quote()
                                    .plus(At(e.sender))
                                    .plus(q)
                            )
                        } else {
                            e.group.sendMessage("不支持的链接，目前只支持：抖音、皮皮搞笑")
                        }
                    } else {
                        // 链接为空
                        e.group.sendMessage("请输入要解析的链接，目前只支持：抖音、皮皮搞笑")
                    }
                }
            }

        }
        logger.info("插件加载完毕！耗时: " + (System.currentTimeMillis() - start) + "ms")
    }
}